import {
    getSingerAxios,
    getSingerListAxios,
    getSingerMusicListAxios,
    getSongInfoAxios,
    getSongListAxios,
    getSongSheetListAxios
} from "@/axios/kugou";
import {kbToMb} from "@/util/utilString";
import {addMark} from "@/music";
import modal from "@/customize/modal";

function getNetMusicInfo(state, data) {
    let music = {};
    let filename = data["filename"];
    let slice = filename.split(" - ");
    music.id = data["hash"];
    music.title = slice[1];
    music.artist = slice[0];
    music.album = "";
    music.size = kbToMb(data["filesize"]);

    // 网络歌曲
    music.type = "NET";
    // 是否正在播放
    music.isPlaying = false;
    // 是否被选中
    music.isSelected = false;

    addMark([music]);

    state.musicList.push(music);
}

export default {
    // 开启命名空间
    namespaced: true,
    getters: {
        getMusicList(state) {
            addMark(state.musicList);
            return state.musicList;
        }
    },
    actions: {
        /**
         * 标题：与axios通信，异步请求获取歌单
         *
         * 变量：type，类型<br />
         * 变量：page，第几页<br />
         * 变量：num，请求数量，最大值为30<br />
         */
        async getSongSheetListNetStore(context, {type, page = 1, num}) {
            let result = await getSongSheetListAxios(page);
            let data = num ? result["plist"].list.info.slice(0, Math.min(num, 30)) : result["plist"].list.info
            data.type = type;
            modal.offModal();
            context.commit("GET_SONG_SHEET_LIST_NET", data);
        },
        // 根据歌单获取歌曲列表
        async getSongListNetStore(context, id) {
            let result = await getSongListAxios(id);
            let infoList = result["list"].list.info;
            let songList = [];
            songList = infoList.filter((info) => {
                return info["privilege"] !== 10
            })
            /*infoList.forEach((info) => {
                if (info["privilege"] !== 10) {

                    let songInfo = getSongInfoAxios(info["hash"]);
                    songInfo.then(
                        (value) => {
                            console.log(value)
                            songList.push(value)
                            context.commit("GET_SONG_LIST_NET", songList);
                        },
                        () => {
                            console.log("请求失败！")
                        }
                    )
                }
            })*/
            context.commit("GET_SONG_LIST_NET", songList);
        },
        // 获取歌曲详细信息
        async getSongNetStore(context, id) {
            let songInfo = await getSongInfoAxios(id);
            context.commit("GET_SONG_NET", songInfo);
        },
        /**
         * 标题：获取歌手列表
         *
         * 变量：type，类型<br />
         * 变量：page，第几页<br />
         * 变量：num，请求数量，最大值为15<br />
         * 变量：info，是否请求歌手详细信息<br />
         */
        async getSingerListNetStore(context, {type, page = 1, num, info = false}) {
            let singerListInfo = await getSingerListAxios(page);
            let data = num ? singerListInfo.data.info.slice(0, Math.min(num, 15)) : singerListInfo.data.info;
            if (info) {
                for (let i = 0; i < data.length; i++) {
                    let s = await getSingerAxios(data[i].singerid);
                    data[i].info = s?.data?.intro;
                }
            }
            data.type = type;
            modal.offModal();
            context.commit("GET_SINGER_LIST_NET", data);
        },
        // 获取歌手详细信息
        async getSingerNetStore(context, id) {
            let singerInfo = await getSingerAxios(id);
            context.commit("GETSINGERNET", singerInfo);
        },
        // 获取歌手详细信息
        async getSingerInfoNetStore(context, singer) {
            let singerInfo = await getSingerAxios(singer.id);
            singer.info = singerInfo?.data?.intro;
            return singerInfo?.data?.intro;
        },
        // 获取歌手下面的歌曲
        async getSingerMusicListNetStore(context, id) {
            let singerMusicList = await getSingerMusicListAxios(id);
            context.commit("GET_SINGER_MUSIC_LIST_NET", singerMusicList.data.info);
        },
    },
    mutations: {
        EMPTY(state) {
            state.musicList.length = 0;
        },
        SINGER_LIST_EMPTY(state) {
            state.singerList.length = 0;
        },
        SONG_SHEET_LIST_EMPTY(state) {
            state.songSheetList.length = 0;
        },
        // 跟具id修改state.isPlay状态
        SET_MUSIC_BY_ID(state, music) {
            // 变更状态
            if (music.id) {
                try {
                    state.musicList.forEach((m) => {
                        if (music.id === m.id) {
                            m.isPlay = false;
                            throw new Error('true');
                        }
                    })
                } catch (e) {
                    if (e.message !== "true") throw e
                }
            }
        },
        GET_SONG_SHEET_LIST_NET(state, data) {
            for (let i = 0; i < data.length; i++) {
                let music = {};
                music.id = data[i]["encode_id"];
                music.title = data[i]["specialname"];
                music.user = data[i]["username"];
                music.playCount = data[i]["play_count_text"];
                music.img = data[i]["imgurl"]?.replace("{size}", 150);
                if (data.type === "home") {
                    state.songSheetListHome.push(music);
                } else {
                    state.songSheetList.push(music);
                }
            }
        },
        GET_SONG_LIST_NET(state, data) {
            for (let i = 0; i < Math.min(100, data.length); i++) {
                if (data[i]["price"] > 0) {
                    continue;
                }
                getNetMusicInfo(state, data[i]);
            }
        },
        GET_SONG_NET(state, data) {
            let music = {};
            // 歌曲id
            music.id = data["hash"];
            // 歌曲标题
            music.title = data["songName"];
            // 歌曲作者
            music.artist = data["author_name"];
            // 专辑
            music.album = data["songName"];
            // 大小
            music.size = kbToMb(data["fileSize"]);
            // 路径
            music.url = data["url"];
            // 封面
            music.image = data["imgUrl"]?.replace("{size}", 150);
            // 是否有异常
            music.error = data["error"];
            // 网络歌曲
            music.type = "NET";

            //addMark([music]);

            state.song = music;
        },
        GET_SINGER_LIST_NET(state, data) {
            for (let i = 0; i < data.length; i++) {
                let singer = {};
                singer.id = data[i].singerid;
                singer.name = data[i].singername;
                singer.head = data[i].imgurl?.replace("{size}", 150);
                singer.info = data[i].info;
                singer.listener = data[i].fanscount;
                singer.heat = data[i].heat;
                singer.is_settled_author = data[i].is_settled;

                if (data.type === "home") {
                    singer.listener = singer.listener ? Math.ceil(parseInt(singer.listener) / 10000) : 0;
                    singer.heat = singer.heat ? Math.ceil(parseInt(singer.heat) / 10000) : 0;
                    state.singerListHome.push(singer);
                } else {
                    state.singerList.push(singer);
                }
            }
        },
        GETSINGERNET(state, data) {
            state.singer.info = data?.data?.intro;
        },
        GET_SINGER_MUSIC_LIST_NET(state, data) {
            for (let i = 0; i < Math.min(100, data.length); i++) {
                if (data[i]["price"] > 0) {
                    continue;
                }
                getNetMusicInfo(state, data[i]);
            }
            addMark(state.musicList);
            //console.log("musicList =", musicList)
            //state.singerMusicList = musicList;
        },
    },
    state() {
        return {
            page: 1,
            musicList: [],
            songSheetListHome: [],
            songSheetList: [],
            songList: [],
            song: {},
            singerListHome: [],
            singerList: [],
            singer: {},
            singerMusicList: [],
        }
    },
}
